<template>
    <div class="rmDir-prompt 
    d-flex flex-column justify-content-center align-items-center">
        <div class="rmDir-prompt-title">
            确定，删除 {{ 
                $store.getters.getRmDir ? $store.getters.getRmDir.title : ""  }} ?
        </div>
        <div class="rmDir-prompt-opr">
            <button type="button" class="m-1 btn btn-secondary"
            @click="cancle"
            >取消</button>
            <button type="button" class="m-1 btn btn-danger"
            @click="deleteDir"
            >确定</button>
        </div>
    </div>
</template>

<script>
import { removeDeratory } from "../../../../api/interface/filesystem.js";
import { freshToken } from "../../../../api/interface/token.js";

export default {
    
    data(){
        return {
            dirItem: "",
        };
    },
    inject:["noteAgent"],
    methods: {
        deleteDir(){
            let that = this;
            let item = this.$store.getters.getRmDir;
            if( item ){
                console.log(item);
                removeDeratory(item).then(res=>{
                    
                    //删除顶层目录的后续工作
                    if( item.top_id ){
                         //重新获取顶层目录
                        this.refetchTopdirData( function(){
                            //当前顶层目录下的子目录内容
                            that.refetchCurrentTopDirData();
                        });
                    }

                    //删除子目录的后续工作
                    if( item.inner_id ){
                        this.refetchCurrentTopDirData();
                    }
                    if( item.art_id ){
                        this.refetchCurrentTopDirData();
                    }
                    // 如果顶层目录获取为空需要将当前路径和显示子目录模块的数据清空
                    
                    //关闭弹层
                    this.cancle();
                }).catch(err=>{
                    this.$httpError.checkResStatus(err.data,function(){
                    
                    //重新请求生成token
                    freshToken().then(res=>{
                        
                        localStorage.setItem("token", res.data.data.token);
                        that.deleteDir(  );
                    })
                },that);
                });
            }

        },
        //重新加载顶层目录
        refetchTopdirData( fn ){
            let note = this.noteAgent();
            note.$refs.noteMenu.fatchData(0,20 , fn);
        },
        //重新获取子目录内容
        refetchCurrentTopDirData(){
            let note = this.noteAgent();
            let target = this.$store.getters.getEndOfPathOfNotebookItem;
            note.$refs.exploreFile.fatchData( target );//0,20);
        },
        //取消删除
        cancle(){
            this.$store.commit("resetRmItem");
            this.noteAgent().popup.rmDir = false;
        }
    },
}
</script>

<style lang="scss">

.rmDir-prompt{
    padding: 1em;
    .rmDir-prompt-title{
        font-size: 1.2em;
    }
    .rmDir-prompt-opr{
        margin-top: .5em;
    }
}
</style>